package shims

import "substr"
import "regexp"
import "os"

//wrap regexp.Regexp to add a Finder-style Find method
type Regexp struct { //implicitly a Matcher
	*regexp.Regexp
}

//wrapper around regexp.Compile to return a shims.Regexp which is a
//substr.Matcher and substr.Finder (regexp.Regexp is already a substr.Matcher)
func Compile(rgx string) (*Regexp, os.Error) {
	r, e := regexp.Compile(rgx)
	return &Regexp{r}, e
}

//wrapper around regexp.MustCompile to return a shims.Regexp which is a
//substr.Matcher and substr.Finder
func MustCompile(rgx string) *Regexp {
	return &Regexp{regexp.MustCompile(rgx)}
}

func (r *Regexp) Find(s []byte, n int) *substr.S {
	out := substr.New(s, 0)
	out.Subs = r.FindAllIndex(s, n)
	return out
}
